package com.example.demo.controller.wx;

import com.example.demo.util.FileUtil;
import com.example.demo.util.RestRequestUtil;
import com.example.demo.vo.ResultVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @Description:
 * @author: liyuan
 * @data 2020-12-08 19:45
 */
@Deprecated
@RestController
public class CVCNNController {
    @Value("${test.rootPath}")
    private String rootPath;
    @Value("${test.projectDir}")
    private String projectDir;
    private Logger logger = LoggerFactory.getLogger(WxFaceRegController.class);
    @Autowired
    private RestTemplate restTemplate;

    @PostMapping("CVCNN")
    public ResultVO cvcnn(@RequestParam("picture") MultipartFile picture) throws IOException, InterruptedException {
        String fileUrl = FileUtil.uploadFile(picture, rootPath, projectDir,
                "Face", "Reg");
        String pictureUrl = rootPath + fileUrl;
        logger.info("图片位置：{}", pictureUrl);
        return new ResultVO(1000, dJangoRestServer(pictureUrl));
    }

    private String dJangoRestServer(String picture) {
        HttpHeaders header = RestRequestUtil.getHeader(MediaType.MULTIPART_FORM_DATA);
        //上传文件
        FileSystemResource resource = new FileSystemResource(new File(picture));
        MultiValueMap<String, Object> multiValueMap = RestRequestUtil.getEntityByMultiForm();
        multiValueMap.add("picture", resource);
        HttpEntity entity = RestRequestUtil.getEntity(multiValueMap, header);
        String res = restTemplate.postForObject("http://127.0.0.1:8000/yolo", entity, String.class);
        return res;
    }
}
